package chu.dejian.conf;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class BLEPreference {
	
	private SharedPreferences sharedPreferences;
	private Editor preferenceEditor;
	private static BLEPreference self;

	public void init(Context context){
		this.sharedPreferences = context.getSharedPreferences(PREFRENCE_NAME, PREFENCE_MODE);
		this.preferenceEditor = this.sharedPreferences.edit();
	}

	public synchronized static BLEPreference getInstance(){
		if(self == null){
			self = new BLEPreference();
		}
		return self;
	}
	
	
	private static final String PREFRENCE_NAME = "dejian_ble";
	private static final int PREFENCE_MODE = Context.MODE_PRIVATE;
	
	private final static String ACCOUNT = "account";
	private final static String PASSWORD = "password";
	private final static String TOKEN = "token";
	private final static String BINDED_DEVICE_MAC = "mac";
	private final static String BINDED_DEVICE_NAME = "name";
	private final static String LAST_CHECKED_RECORD_PAGE = "measurement_page";
	//-----------------------------------------------------------------
	//             GET
	//-----------------------------------------------------------------

	public int getMeasurementPage(){
		return sharedPreferences.getInt(LAST_CHECKED_RECORD_PAGE,1);
	}

	public String getToken(String defaultToken){
		return sharedPreferences.getString(TOKEN, defaultToken);
	}

	public String getPassword(String defaultValue) {
		return sharedPreferences.getString(PASSWORD, defaultValue);
	}
	public String getAccount(String defaultValue) {
		return sharedPreferences.getString(ACCOUNT, defaultValue);
	}

	public String getDeviceName(String defaultName){
		return sharedPreferences.getString(BINDED_DEVICE_NAME,defaultName);
	}

	public String getDeviceMac(String defaultMac){
		return sharedPreferences.getString(BINDED_DEVICE_MAC,defaultMac);
	}
	//-----------------------------------------------------------------
	//             SET
	//-----------------------------------------------------------------
	public void setMeasurementPage(int page){
		preferenceEditor.putInt(LAST_CHECKED_RECORD_PAGE,page);
		preferenceEditor.commit();
	}

	public void setToken(String token){
		preferenceEditor.putString(TOKEN,token);
		preferenceEditor.commit();
	}

	public void setAccount(String account){
		preferenceEditor.putString(ACCOUNT,account);
		preferenceEditor.commit();
	}
	
	public void setPassword(String passwd){
		preferenceEditor.putString(PASSWORD,passwd);
		preferenceEditor.commit();
	}

	public void setDeviceName(String deviceName){
		preferenceEditor.putString(BINDED_DEVICE_NAME,deviceName);
		preferenceEditor.commit();
	}

	public void setBindedDeviceMac(String deviceMac){
		preferenceEditor.putString(BINDED_DEVICE_MAC,deviceMac);
		preferenceEditor.commit();
	}

}
